package cn.tq365.service.baidu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import cn.tq365.mapper.BaiduCampaignMapper;
import cn.tq365.util.baidu.SyncServiceFactory;

import com.baidu.api.client.core.ClientBusinessException;
import com.baidu.api.client.core.ObjToStringUtil;
import com.baidu.api.client.core.ResHeaderUtil;
import com.baidu.api.client.core.VersionService;
import com.baidu.api.sem.common.v2.ResHeader;
import com.baidu.api.sem.sms.v2.CampaignService;
import com.baidu.api.sem.sms.v2.CampaignType;
import com.baidu.api.sem.sms.v2.GetAllCampaignRequest;
import com.baidu.api.sem.sms.v2.GetAllCampaignResponse;

/**
 * 百度账户操作服务类
 * 
 * @author lili
 * 
 */
@Service
@Scope("prototype")
public class BaiduCampaignService {
	@Resource
	BaiduCampaignMapper syncCampaignMapper;

	/**
	 * 添加
	 * 
	 * @param account
	 * @throws Exception
	 */
	public void add(String userName, String password) throws Exception {
		try {
			VersionService factory = SyncServiceFactory.getInstance(userName,
					password);
			CampaignService service = factory.getService(CampaignService.class);
			GetAllCampaignRequest parameters = new GetAllCampaignRequest();
			GetAllCampaignResponse ret = service.getAllCampaign(parameters);
			ResHeader rheader = ResHeaderUtil.getResHeader(service, true);
			if (rheader.getStatus() == 0) {
				System.out.println("getAllCampaign.result\n"
						+ ObjToStringUtil.objToString(ret));
				// syncCampaignMapper.add(ret.getAccountInfoType());
				List<CampaignType> list = ret.getCampaignTypes();
				Map map = new HashMap();
				map.put("accountId", 3170405);
				for (CampaignType cmp : list) {
					map.put("cmp", cmp);
					syncCampaignMapper.add(map);
				}
			} else {
				throw new ClientBusinessException(rheader, ret);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}
	/**
	 * 查询
	 * 
	 * @return
	 * @throws Exception
	 */
	// public List<AccountInfoType> query() throws Exception {
	// try {
	// return syncAccountMapper.query();
	// } catch (Exception e) {
	// e.printStackTrace();
	// throw e;
	// }
	// }
}
